#include <time.h>
#include <d3dx9.h>
#include "Scene1.h"
#include "Utils.h"

#define BACKGROUND_FILE "Images/BackgroundGame.png"
#define SPEED_X 0.5f
#define SPEED_Y	0.75f

Scene1::Scene1(int _nCmdShow):CGame(_nCmdShow)
{
	camera = NULL;
	bg = NULL;
}

Scene1::~Scene1()
{
	if(camera != NULL)
		delete camera;
}

void Scene1::LoadResources(LPDIRECT3DDEVICE9 d3ddv)
{
	srand((unsigned)time(NULL));

	D3DXCreateSprite(d3ddv,&G_SpriteHandler);
	HRESULT res = D3DXCreateSprite(d3ddv,&G_SpriteHandler);

	camera = new CCamera();
	bg = new QBackground(1);
	bg->LoadTree();
	simon = new Simon(50, 100);
	qGameObject = new QGameObject("Resources/Maps/1.txt");
}

void Scene1::RenderFrame(LPDIRECT3DDEVICE9 d3ddv, int deltaTime)
{
	qGameObject->Update(deltaTime);
	bg->GetTreeObject(camera->viewport.x, camera->viewport.y);
	simon->Update(deltaTime);
	camera->UpdateCamera(simon->posX);
	
	simon->Collision(*(qGameObject->_staticObject), deltaTime);
	simon->Collision(*(qGameObject->_dynamicObject), deltaTime);
	qGameObject->Collision(deltaTime);

	G_SpriteHandler->Begin(D3DXSPRITE_ALPHABLEND);

	bg->Draw(camera);
	qGameObject->Draw(camera);
	simon->Draw(camera);

	G_SpriteHandler->End();
}

void Scene1::ProcessInput(LPDIRECT3DDEVICE9 d3ddv, int t)
{
	if (IsKeyDown(DIK_RIGHT) || IsKeyDown(DIK_D))
	{
		simon->TurnRight();
	}	
	else 
		if (IsKeyDown(DIK_LEFT) || IsKeyDown(DIK_A))
		{
			simon->TurnLeft();
		}
		else 
		{
			simon->Stop();
		}
}

void Scene1::OnKeyDown(int KeyCode)
{
	switch (KeyCode)
	{
	case DIK_SPACE:
		simon->Jump();
		break;
	}
}